﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Employee_UpdateEmployee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameEmployee"] == null)
            {
                Response.Redirect("../Home/HomePage.aspx");
            }
            else
            {
                String userName = Session["userNameEmployee"].ToString();
                String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
                SqlConnection sqlConnect = new SqlConnection(connect);
                sqlConnect.Open();
                SqlCommand sqlCommand = new SqlCommand("Select firstName,lastName,address,contactNo,state,country,city from EmpRegister where userName='" + userName + "'", sqlConnect);
                SqlDataReader reader = sqlCommand.ExecuteReader();
                reader.Read();
                txtFirstName.Text = reader.GetString(0);
                txtLastName.Text = reader.GetString(1);
                txtAddress.Text = reader.GetString(2);
                txtContact.Text = reader.GetString(3);
                txtState.Text = reader.GetString(4);
                txtCountry.Text = reader.GetString(5);
                txtCity.Text = reader.GetString(6);
                sqlConnect.Close();
                
            }

        }
        ClearError();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean check = false;
        if (txtFirstName.Text.Equals(""))
        {
            Label2.Visible = true;
            check = true;
        }
        if (txtLastName.Text.Equals(""))
        {
            Label10.Visible = true;
            check = true;
        }

        if (txtAddress.Text.Equals(""))
        {
            Label4.Visible = true;
            check = true;
        }
        if (txtContact.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        if (txtState.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        }
        if (txtCountry.Text.Equals(""))
        {
            Label10.Visible = true;
            check = true;
        }
        if (txtCity.Text.Equals(""))
        {
            Label12.Visible = true;
            check = true;
        }
        if (!check)
        {
            String userName = Session["userNameEmployee"].ToString();
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("Update EmpRegister Set  firstName=@firstName,lastName=@lastName,address=@address,contactNo=@contactNo,state=@state,country=@country,city=@city where username='" + userName + "'", sqlConnect);
            sqlCommand.Parameters.AddWithValue("@firstName", txtFirstName.Text);
            sqlCommand.Parameters.AddWithValue("@lastName", txtLastName.Text);
            sqlCommand.Parameters.AddWithValue("@address", txtAddress.Text);
            sqlCommand.Parameters.AddWithValue("@contactNo", txtContact.Text);
            sqlCommand.Parameters.AddWithValue("@state", txtState.Text);
            sqlCommand.Parameters.AddWithValue("@country", txtCountry.Text);
            sqlCommand.Parameters.AddWithValue("@city", txtCity.Text);
            sqlCommand.ExecuteNonQuery();
            sqlConnect.Close();
            success.Visible = true;

            txtAddress.Text = "";
            txtCity.Text = "";
            txtContact.Text = "";
            txtCountry.Text = "";
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtState.Text = "";
        }
    }
    public void ClearError()
    {
        Label2.Visible = false;
        Label4.Visible = false;
        Label6.Visible = false;
        Label8.Visible = false;
        Label10.Visible = false;
        Label12.Visible = false;
        Label14.Visible = false;
        success.Visible = false;
    }

}